//
// Created by ziya on 2023/11/28.
//

#ifndef ZIYA_MULTICORE_X64_GITEE_GDT_H
#define ZIYA_MULTICORE_X64_GITEE_GDT_H

#include "types.h"

#define TSS_INDEX 8

typedef struct {
    ushort  limit;
    int64   base;
} __attribute__((packed)) gdtr_data_t;

typedef struct tss {
    uint32_t reserved0;
    uint64_t rsp0;      // Ring 0 堆栈指针
    uint64_t rsp1;      // Ring 1 堆栈指针（通常不使用）
    uint64_t rsp2;      // Ring 2 堆栈指针（通常不使用）
    uint64_t reserved1;
    uint64_t ist1;      // 中断堆栈表指针1
    uint64_t ist2;      // 中断堆栈表指针2
    uint64_t ist3;      // 中断堆栈表指针3
    uint64_t ist4;      // 中断堆栈表指针4
    uint64_t ist5;      // 中断堆栈表指针5
    uint64_t ist6;      // 中断堆栈表指针6
    uint64_t ist7;      // 中断堆栈表指针7
    uint64_t reserved2;
    uint16_t reserved3;
    uint16_t iomap_base; // I/O 映射基址
} __attribute__((packed)) tss_t;

typedef struct tss_descriptor {
    uint16_t limit_low;
    uint16_t base_low;
    uint8_t base_mid;
    uint8_t flags1;
    uint8_t limit_high_and_flags2;
    uint8_t base_high;
    uint32_t base_upper;
    uint32_t reserved;
} __attribute__((packed)) tss_descriptor_t;

void gdt_init();

void install_tss();

#endif //ZIYA_MULTICORE_X64_GITEE_GDT_H
